//
//  NRWHotCarBrandView.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/22.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWHotCarBrandView.h"
#import "NRWCarBrandModel.h"
/// 热门品牌
@interface NRWHotCarBrandItemView : UIView

@property (strong, nonatomic) UIImageView *iconImageView;

@property (strong, nonatomic) UILabel *nameLabel;

+ (instancetype)itemViewWithImageUrl:(NSString *)url name:(NSString *)name;

@end

@implementation NRWHotCarBrandItemView

+ (instancetype)itemViewWithImageUrl:(NSString *)url name:(NSString *)name {
    
    NRWHotCarBrandItemView *item = [[NRWHotCarBrandItemView alloc] init];
    if ([url hasPrefix:@"http"]) {
        [item.iconImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil];
    } else {
        item.iconImageView.image = getImage(url);
    }
    item.nameLabel.text = name;
    return item;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.iconImageView = [[UIImageView alloc] init];
        self.iconImageView.opaque = YES;
        
        self.nameLabel = [[UILabel alloc] init];
        self.nameLabel.textColor = getColor(@"#606060");
        self.nameLabel.font = getThemeFont(13);
        
        self.addSubview(self.iconImageView).addSubview(self.nameLabel);
        
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left);
            make.centerY.equalTo(self.mas_centerY);
            make.width.height.equalTo(@20);
        }];
        
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);
            make.left.equalTo(self.iconImageView.mas_right).offset(5);
        }];
    }
    return self;
}

@end


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation NRWHotCarBrandView

- (void)setHotBrandModel:(NRWCarBrandModel *)hotBrandModel {
    
    _hotBrandModel = hotBrandModel;
    
    [self setupUI];
}

- (void)setupUI {
    
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:NRWHotCarBrandItemView.class]) {
            [view removeFromSuperview];
        }
    }
    
    NSMutableArray <NRWHotCarBrandItemView *>*arrM = [NSMutableArray arrayWithCapacity:8];
    
    for (NSInteger i = 0; i < self.hotBrandModel.result.count; i++) {
        NRWCarBrandResult *result = self.hotBrandModel.result[i];
        NRWHotCarBrandItemView *item = [NRWHotCarBrandItemView itemViewWithImageUrl:[NSString stringWithFormat:@"%@%@", imageApi(), result.fastBrandLogo] name:result.fastBrandName];
        [self addSubview:item];
        item.tag = i;
        [arrM addObject:item];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDidClick:)];
        [item addGestureRecognizer:tap];
    }
    
    NRWHotCarBrandItemView *item = [NRWHotCarBrandItemView itemViewWithImageUrl:@"home_moreBrand" name:@"更多"];
    [self addSubview:item];
    item.tag = 7;
    [arrM addObject:item];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDidClick:)];
    [item addGestureRecognizer:tap];
    
    NSArray *first = [arrM subarrayWithRange:NSMakeRange(0, 4)];
    NSArray *second = [arrM subarrayWithRange:NSMakeRange(4, 4)];
    
    [first mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:getAdaptationWidth(20) leadSpacing:getAdaptationWidth(35) tailSpacing:getAdaptationWidth(35)];
    [first mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.height.equalTo(@40);
    }];
    
    [second mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:getAdaptationWidth(20) leadSpacing:getAdaptationWidth(35) tailSpacing:getAdaptationWidth(35)];
    [second mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(arrM.firstObject.mas_bottom).offset(10);
        make.height.equalTo(@40);
    }];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)addTapAction:(UIView *)view {
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDidClick:)];
    [view addGestureRecognizer:tap];
}

- (void)tapDidClick:(UITapGestureRecognizer *)tap {
    
    if ([self.delegate respondsToSelector:@selector(hotCarBrandView:didClickAtIndex:)]) {
        
        [self.delegate hotCarBrandView:self didClickAtIndex:tap.view.tag];
    }
}

@end
